<?php
class Activity_lottery_model extends BZ_Model
{
    protected $table   = 'activity_lottery';
    protected $table2  = 'user';
    protected $table3  = 'lottery_reseller';

	/**
     * 查询抽奖活动总数
     * @param array $getData
     */
    public function total($getData = array())
    {
        $this->db->select('lottery_id');
		$this->db->from($this->table.' AS activity_lottery');
        $this->db->where('`activity_lottery`.`uid`', $getData['uid']);
        $this->db->where('audit_state <>', 4); //已删除
        if (!empty($getData['lottery_name'])) {
            $this->db->like('`activity_lottery`.`lottery_name`', $getData['lottery_name']);
        }
        if (!empty($getData['status'])) {
            $this->db->where('activity_lottery.status', $getData['status']);
        }
        return $this->db->count_all_results();
    }

	/**
     * 查询抽奖活动列表
	 * @param number $page_num
	 * @param number $num
     * @param array $getData
     */
    public function page_list($page_num, $num, $getData = array())
    {
		$this->db->select('activity_lottery.uid, lottery_id, lottery_name, lottery_sum,winning_num, audit_state, on_line, status,activity_lottery.created_at, user_name, start_date, end_date');
		$this->db->from($this->table.' AS activity_lottery');
		$this->db->where('audit_state <>', 4); //已删除

        $this->db->where('`activity_lottery`.`uid`', $getData['uid']);
        if (!empty($getData['lottery_name'])) {
            $this->db->like('`activity_lottery`.`lottery_name`', $getData['lottery_name']);
        }
        if (!empty($getData['status'])) {
            $this->db->where('activity_lottery.status', $getData['status']);
        }

        $this->db->order_by('activity_lottery.lottery_id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }

    public function findByLotteryId($lottery_id, $uid)
    {
        $this->db->select('activity_lottery.uid, lottery_id, lottery_name, lottery_sum, winning_num, titles, on_line, status, winners_total, user_total, start_date, end_date, details, adverts, winners_details_pages');
        $this->db->from($this->table);
        $this->db->where('lottery_id', $lottery_id);
        $this->db->where('uid', $uid);
        $this->db->where('audit_state <>', 4); //已删除
        $this->db->limit(1);
        return $this->db->get();
    }

    /**
     * 根据uid查找参与抽奖用户
     */
    public function findByUid($uid)
    {
        $this->db->select('lottery_id, lottery_name');
        $this->db->from($this->table);
        $this->db->where('uid', $uid);
        $this->db->where('audit_state <>', 4); //已删除
        return $this->db->get();
    }

    /**
     * 更新抽奖活动
     * @param unknown $lottery_id
	 * @param array $data
     */
	public function updateLottery($postData)
	{
        $data = array(
            'lottery_name' => $postData['lottery_name'],
            'status'       => $postData['status'],
            'lottery_sum'  => $postData['lottery_sum'],
            'titles'       => $postData['titles'],
            'on_line'      => $postData['on_line'],
            'winning_num'  => $postData['winning_num'],
            'start_date'   => $postData['start_date']. ' 00:00:01',
            'end_date'     => $postData['end_date']. ' 23:59:59',
            'details'      => $postData['details'],
            'adverts'      => $postData['adverts'],
            'winners_details_pages'      => $postData['winners_details_pages']
        );
		$this->db->where('lottery_id', $postData['lottery_id']);
        $this->db->where('uid', $postData['uid']);
        return $this->db->update($this->table,$data);
	}

	/**
     * 插入抽奖活动
	 * @param array $data
     */
	public function insertLottery($postData)
	{
        $data = array(
            'uid'          => $postData['uid'],
            'user_name'    => $postData['user_name'],
            'lottery_name' => $postData['lottery_name'],
            'status'       => $postData['status'],
            'lottery_sum'  => $postData['lottery_sum'],
            'winning_num'  => $postData['winning_num'],
            'titles'    => $postData['titles'],
            'on_line'      => $postData['on_line'],
            'audit_state'  => 1, //待审核
            'winners_total'=> 0,
            'user_total'   => 0,
            'start_date'   => $postData['start_date']. ' 00:00:01',
            'end_date'     => $postData['end_date']. ' 23:59:59',
            'details'      => $postData['details'],
            'adverts'      => $postData['adverts'],
            'winners_details_pages'      => $postData['winners_details_pages'],
            'created_at'   => date("Y-m-d H:i:s", time())
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
	}

	/**
     * 删除抽奖活动 假装删除了
     */
	public function deleteLottery($lotteryId, $uid)
	{
        $this->db->set('audit_state', 4);
        $this->db->where('lottery_id', $lotteryId);
        $this->db->where('uid', $uid);
		return $this->db->update($this->table);
	}

     /**
     * 根据ID 查找   有效的活动
     */
    public function findLotteryCache($lotteryId, $uid)
    {
        $today = date('Y-m-d');
        $this->db->select('activity_lottery.uid, lottery_id, lottery_name, audit_state, lottery_sum, winning_num, status, titles, on_line, user_name, winners_total, user_total, created_at, start_date, end_date, details, adverts, winners_details_pages');
        $this->db->from($this->table.' AS activity_lottery');
        $this->db->where('activity_lottery.lottery_id', $lotteryId);
        $this->db->where('uid', $uid);
        $this->db->where('status', 1);//上架
        $this->db->where('audit_state', 2);//审核通过
        $this->db->limit(1);
        return $this->db->get();
    }
}